
import { _decorator, Component, Node, Vec3, Quat, ParticleSystemComponent, math, ITriggerEvent, BoxColliderComponent, SphereColliderComponent, Prefab, TriggerEventType, CCFloat, utils, RigidBody } from 'cc';
import { poolManager } from '../../../../Framework/Scripts/Managers/poolManager';
import { CocosUtils } from '../../../../Framework/Scripts/Utils/CocosUtils';
import myUtil from '../../../../Framework/Scripts/Utils/myUtil';
import { buildingCtrl } from '../buildingCtrl';
import { enemyBase } from '../enemyBase';
import { playerBase } from '../playerBase';
import { enemySkillBase } from './enemySkillBase';


//单个火球类型的技能 可以是多个
const { ccclass, property } = _decorator;
@ccclass('boss_arrow')
export class boss_arrow extends enemySkillBase {
    //是不是多个
    @property({ displayName: "是不是散射技能" })
    private isSanshe: boolean = true;



    private attackTarget: Node = null!
    public isAutoRotate: boolean = true;//箭是否自动调整角度
    public isArrowLaunch: boolean = false;//箭是否弹射

    //private _ndBody: Node = null!;//放弓箭特效的节点
    private _curSpeed: number = 0;//当前速度
    private _targetSpeed: number = 0;//目标速度
    private _oriPos: Vec3 = null!;//初始默认位置
    private _oriEulerAngles: Vec3 = null!//初始默认角度
    private _offsetPos: Vec3 = new Vec3();//和玩家之间的向量差
    private _curWorPos: Vec3 = new Vec3();//当前节点世界坐标
    private _disappearRange: number = 625;//箭节点超过玩家这个范围则隐藏
    private _isLoadEffectOver: boolean = false;//是否已经加载完所有特效
    private _isNeedShowEffect: boolean = false;//是否需要特效
    private _targetWorPos: Vec3 = new Vec3();//箭的下次目标位置
    private _curEulerAngles: Vec3 = new Vec3();//当前角度
    private _oriForward: Vec3 = null!;//初始朝向
    private _curForward: Vec3 = new Vec3();//当前朝向
    private _releaseWorPos: Vec3 = new Vec3();//技能释放位置的世界坐标
    private _offsetPos_1: Vec3 = new Vec3();//向量差
    private _offsetPos_2: Vec3 = new Vec3();//向量差
    private _cross: Vec3 = new Vec3();//两个向量叉乘
    private _colliderCom: BoxColliderComponent = null!;//

    private startPos: Vec3 = new Vec3()





    onLoad() {

        this._colliderCom = this.node.getComponent(BoxColliderComponent) as BoxColliderComponent;
        this._colliderCom.on('onTriggerEnter', this._onTriggerEnterCb, this);
    }

    onEnable() {

    }

    onDisable() {
        //this._colliderCom.off('onTriggerEnter', this._onTriggerEnterCb, this);
    }

    start() {

        // [3]
    }

    /**
    * 初始化 
    */
    public init(howHit: Node, speed: number, releaseWorPos: Vec3, isPreload: boolean = false, startPos: Vec3) {


        this.howHit = null
        this.howHit = howHit
 







        this.startPos = startPos
        this._releaseWorPos.set(releaseWorPos);


        this._isLoadEffectOver = false;
        this._isNeedShowEffect = false;
        this._disappearRange = isPreload ? 5 : 625;

        this.isArrowLaunch = false;

        if (!this._oriPos) {
            this._oriPos = this.node.position.clone();
        }

        if (!this._oriEulerAngles) {
            this._oriEulerAngles = this.node.eulerAngles.clone();
        }

        if (!this._oriForward) {
            this._oriForward = this.node.forward.clone();
        }

        this.node.active = false;
        this.node.setPosition(this._oriPos);
        this.node.eulerAngles = this._oriEulerAngles;
        this._curForward.set(this._oriForward);

        this._targetSpeed = speed;
        this._curSpeed = speed * 0.5;



        this.node.active = true;
    }



    /**
     *  回收弓箭组，在weapon/arrow下
     *
     * @memberof Arrow
     */
    public recycleArrowGroup() {
        if (this.node.parent) {
            // console.log("全部回收了");

            poolManager.instance.putNode(this.node.parent);
        }
    }

    /**
     * 击中目标,隐藏箭
     *
     * @memberof Arrow
     */
    public hideArrow() {

        if (!this.node.parent) {
            //  console.log("没有父节点");


            return;
        }



  

        this.node.active = false;

        //如果弓箭组里所有的箭都隐藏了则回收整个弓箭组
        let isAllArrowHide = this.node.parent?.children.every((ndArrow: Node) => {
            return ndArrow.active === false;
        })

        if (isAllArrowHide) {

            this.recycleArrowGroup();
        }
    }


    private _onTriggerEnterCb(event: ITriggerEvent) {
        const other = event.otherCollider.node;
        if (other.getComponent(enemyBase)) {
            if (other.getComponent(enemyBase).obj_type == this.howHit.getComponent(enemyBase).obj_type) {
                return
            }
        }

        if (this.isSanshe) {
            this.hideArrow()
        } else {
            poolManager.instance.putNode(this.node)
        }




   





     
   

       this.hit(other)


        if (this.hitNode) {
   

            const hit: Node = poolManager.instance.getNode(this.hitNode, other.parent)
            const pos: Vec3 = this.node.getWorldPosition()
            hit.setWorldPosition(new Vec3(pos.x, 3, pos.z))
            myUtil.playEffect(hit, false, true, 1, true)
        }

  

    }
    update(deltaTime: number) {





        //朝forward方向飞行
        this._curSpeed = CocosUtils.lerp(this._targetSpeed, this._curSpeed, 0.25);
        this._targetWorPos.set(0, 0, -deltaTime * this._curSpeed);
        this.node.translate(this._targetWorPos, Node.NodeSpace.LOCAL);
        this._curWorPos.set(this.node.worldPosition);

        //超过玩家一定范围则隐藏
        Vec3.subtract(this._offsetPos, this._curWorPos, this.startPos);
        if (this._offsetPos && this._offsetPos.lengthSqr() >= this._disappearRange) {
            if (this.isSanshe) {
                this.hideArrow();
            } else {
                //console.log("不是散射直接删除");

                poolManager.instance.putNode(this.node)
            }

        }
    }
}